<?php
/**
 * Implements hook_token_info().
 */
function custom_token_info() {
  $info = array();

  $info['tokens']['node']['custom_og_image_url'] = array(
    'name' => t('og:image url'),
    'description' => t('Display image url for og:image'),
	'type' => 'node',
  );
  $info['tokens']['node']['custom_og_description'] = array(
    'name' => t('og:description'),
    'description' => t('Create custom description for og:description'),
	'type' => 'node',
  );
  return $info;
}

/**
 * Implements hook_tokens().
 */
function custom_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $replacements = array();
  if ($type == 'node') {
    foreach ($tokens as $name => $original) {
      switch ($name) {
	    case 'custom_og_image_url':
		  $node = $data['node'];
		  $img_url = '';
 		  if (!empty($node->field_thumbnail['und'][0]['uri'])) {
		    $img_url = file_create_url($node->field_thumbnail['und'][0]['uri']);
		  }
		  elseif (!empty($node->field_article_content['und'][0]['value'])) {
			$collection_fields = field_collection_item_load($node->field_article_content['und'][0]['value']);
			if (!empty($collection_fields->field_images['und'][0]['uri'])) {
		      $img_url = file_create_url($collection_fields->field_images['und'][0]['uri']);
			}
		  }
		  elseif (!empty($node->field_image['und'][0]['uri'])) { 
		    // blog or other content type
		    $img_url = file_create_url($node->field_image['und'][0]['uri']);
		  }

		  $replacements[$original] = $img_url;
		  break;

		case 'custom_og_description':
		  $node = $data['node'];
		  $description = '';

		  if(!empty($node->field_article_content['und'][0]['value'])) {

		  	$collection_fields = field_collection_item_load($node->field_article_content['und'][0]['value']);
			if (!empty($collection_fields->field_body['und'][0]['value'])) {
		      $body_text = trim(strip_tags($collection_fields->field_body['und'][0]['value']));
	  		  $description = mb_substr($body_text, 0, 150);
			}
		  }

		  $replacements[$original] = $description;
		  break;
	  }
	}
  }
  return $replacements;
}
